﻿using FCBLL;
using FCModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileConverter.Bas
{
    public partial class CMMParameterManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindGV_CMMParam();
            }
        }

        protected void BindGV_CMMParam() 
        {
            if (string.IsNullOrWhiteSpace(tb_ParameterName.Text))
            {
                GV_CMMParam.DataSource = new BaseRepositoryBLL<CMMParameter>().LoadAllEntities().OrderByDescending(p=>p.modifyDTime).ToList<CMMParameter>();
            }
            else
            {
                GV_CMMParam.DataSource = new BaseRepositoryBLL<CMMParameter>().
                    LoadEntities(p => p.ParameterName.Equals(tb_ParameterName.Text)).OrderByDescending(p=>p.modifyDTime).ToList<CMMParameter>();
            }
            GV_CMMParam.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGV_CMMParam();
        }

        protected void bt_Save_Click(object sender, EventArgs e)
        {
            var cmm = new CMMParameter();
            cmm.ParameterName = tb_ParameterName.Text;
            cmm.isEnable = (ddl_isEnable.SelectedValue == "1" ? true : false);
            cmm.paramSeq = Convert.ToInt32(paramSeq.Text);
            cmm.valueSeq = Convert.ToInt32(valueSeq.Text);
            cmm.modifyDTime = DateTime.Now;
            new BaseRepositoryBLL<CMMParameter>().AddEntities(cmm);
            GV_CMMParam.EditIndex = -1;
            BindGV_CMMParam();
        }

        protected void GV_CMMParam_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void GV_CMMParam_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var btt = new CMMParameter();
            btt.id = Convert.ToInt32(GV_CMMParam.DataKeys[e.RowIndex]["id"].ToString().Trim());
            new BaseRepositoryBLL<CMMParameter>().DeleteEntities(btt);
            GV_CMMParam.EditIndex = -1;
            BindGV_CMMParam();
        }

        protected void GV_CMMParam_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_CMMParam.PageIndex = e.NewPageIndex;
        }

        protected void GV_CMMParam_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var cmm = new CMMParameter();
            //CMMParameter cmm = new BaseRepositoryBLL<CMMParameter>().LoadEntities(p => p.ParameterName.Equals(GV_CMMParam.Rows[e.RowIndex].Cells[1].Text.Trim())).FirstOrDefault();
            cmm.id = Convert.ToInt32(GV_CMMParam.Rows[e.RowIndex].Cells[1].Text.Trim());
            cmm.ParameterName = GV_CMMParam.Rows[e.RowIndex].Cells[2].Text.Trim();
            cmm.isEnable = ((Label)GV_CMMParam.Rows[e.RowIndex].Cells[3].FindControl("L_isEnable")).Text.Trim().ToLower() == "true" ? true : false;
            cmm.paramSeq = Convert.ToInt32(((TextBox)GV_CMMParam.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim());
            cmm.valueSeq = Convert.ToInt32(((TextBox)GV_CMMParam.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim());
            cmm.modifyDTime = DateTime.Now;
            new BaseRepositoryBLL<CMMParameter>().UpdateEntities(cmm);
            GV_CMMParam.EditIndex = -1;
            BindGV_CMMParam();
        }

        protected void GV_CMMParam_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_CMMParam.EditIndex = -1;
            BindGV_CMMParam();
        }

        protected void GV_CMMParam_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_CMMParam.EditIndex = e.NewEditIndex;
            BindGV_CMMParam();
        }

        protected void ddl_isEnable_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DDLqueryType = sender as DropDownList;
            GridViewRow gvr = (GridViewRow)(sender as DropDownList).NamingContainer;
            Label TBqueryType = gvr.FindControl("L_isEnable") as Label;
            TBqueryType.Text = DDLqueryType.SelectedValue;
        }

        protected void GV_CMMParam_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    if (((LinkButton)e.Row.Cells[8].Controls[2]).Text.Equals("删除"))
            //    {
            //        ((LinkButton)e.Row.Cells[8].Controls[2]).Attributes.Add("onclick",
            //            string.Format("javascript:return confirm('您是要删除文件类型为" + e.Row.Cells[0].Text + "的数据吗？')"));
            //    }
            //}

            if ((DropDownList)e.Row.FindControl("ddl_isEnable") != null)
            {
                DropDownList DDLqueryType = (DropDownList)e.Row.FindControl("ddl_isEnable");
                DDLqueryType.SelectedValue = ((Label)e.Row.FindControl("L_isEnable")).Text;
            }
        }
    }
}